//
//  ExchangeViewController.m
//  Findif
//
//  Created by Mac on 2019/4/10.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "ExchangeViewController.h"
#import "MoneyModel.h"
#import "ExchangeTableViewCell.h"
#import "ExchangeDiscountTableViewCell.h"

@interface ExchangeViewController ()<UITableViewDataSource,UITableViewDelegate,ExchangeTableViewCellDelegate,ExchangeDiscountTableViewCellDelegate>

@property (nonatomic, weak)UISegmentedControl *segment;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray<MoneyModel *> * moneyArr;
@property (nonatomic, copy)NSString * type;

@end

@implementation ExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xEFF0F1);
    self.title = @"积分兑换";
    self.type = @"2";
    
    [self loadDataWithType:self.type];
    [self setupUI];
}


- (void)loadDataWithType:(NSString *)type{
    
    [[ZRNetworkTool sharedTool] getIntegralShopListWithType:type WithBlock:^(BOOL success, id responseData) {

        if (success) {
            
            self.moneyArr = [NSArray yy_modelArrayWithClass:[MoneyModel class] json:responseData[@"data"]];
        }
    }];
}

- (void)setMoneyArr:(NSArray<MoneyModel *> *)moneyArr{
    
    _moneyArr = moneyArr;
    
    [self.tableView reloadData];
}


- (void)setupUI{
    
    NSArray *array = [NSArray arrayWithObjects:@"现金兑换",@"优惠券兑换", nil];
    //初始化UISegmentedControl
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:array];
    //设置frame
//    segment.frame = CGRectMake(10, 100, self.view.frame.size.width-20, 30);
    segment.tintColor = ColorWithRGB(0xFFE035);
    segment.selectedSegmentIndex = 0;
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    segment.layer.cornerRadius = 15 * SCREENH6;
    segment.layer.borderWidth = 1.0f;
    segment.layer.borderColor = [ColorWithRGB(0xFFE035) CGColor];
    segment.layer.masksToBounds = YES;
//    segment.tintColor = [UIColor clearColor];
    //    选中的颜色
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:ZRBlackColor} forState:UIControlStateSelected];
    //    未选中的颜色
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];

    
    self.segment = segment;
    //添加到视图
    [self.view addSubview:self.segment];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight + 50 * SCREENH6, kScreenW,kScreenH - topHeight - 50 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 90 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[ExchangeTableViewCell class] forCellReuseIdentifier:@"ExchangeTableViewCell"];
    [self.tableView registerClass:[ExchangeDiscountTableViewCell class] forCellReuseIdentifier:@"ExchangeDiscountTableViewCell"];
    
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self updateData];
//    }];
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.moneyArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"2"]) {
        
        ExchangeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExchangeTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.moneyArr[indexPath.row];
        cell.delegate = self;
        
        return cell;
    }else{
        
        ExchangeDiscountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExchangeDiscountTableViewCell" forIndexPath:indexPath];
        
        cell.model = self.moneyArr[indexPath.row];
        cell.delegate = self;
        
        return cell;
    }
    
}


- (void)viewDidLayoutSubviews{
    
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(topHeight + 10 * SCREENH6);
        make.width.mas_equalTo(200 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
}

-(void)change:(UISegmentedControl *)sender{

    if (sender.selectedSegmentIndex == 0) {
        
        self.type = @"2";
    }else if (sender.selectedSegmentIndex == 1){
        
        self.type = @"1";
    }
    
    [self loadDataWithType:self.type];
}

- (void)clickExchangeButtonID:(NSString *)ID{
    
    [[ZRNetworkTool sharedTool] integralExchangeWithIntegral_rules_id:ID WithBlock:^(BOOL success, id responseData) {
        
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:responseData[@"message"]
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }];
}


- (void)clickExchangeDiscountButtonID:(NSString *)ID{
    
    [[ZRNetworkTool sharedTool] integralExchangeWithIntegral_rules_id:ID WithBlock:^(BOOL success, id responseData) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:responseData[@"message"]
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }];
}

@end
